import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { CreateDeviceDto } from './Dtos/CreateDevice.dto';
import { PageInfo } from './Dtos/PageInfo.dto';
import { Device } from './Entities/device.entity';

@Injectable()
export class AppService {
  constructor(@InjectRepository(Device)
  private readonly _deviceRepository: Repository<Device>) {}

  getHello(): string {
    return 'Hello World!';
  }

  // 获取设备
  async getDevice(pageInfo: PageInfo): Promise<Device []> {
    // 第几页 每页个数
    const data = await this._deviceRepository
      .createQueryBuilder()
      .skip((pageInfo._page - 1) * pageInfo._limit)
      .take(pageInfo._limit)
      .getMany();
    return data;
  }

  // 创建设备
  async createDevice(createDeviceDto: CreateDeviceDto) {
    const device = this._deviceRepository
      .create(createDeviceDto);
    const data = await this._deviceRepository.save(device);
    return data;
  }
}
